package com.luocc.tim.header;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.luocc.tim.R;
import com.luocc.tim.utils.TimUtils;

public class TimHeader extends RelativeLayout {

    private TextView mText;
    private ImageView mImage;

    public static final String TAG = "TimHeader";

    public TimHeader(@NonNull Context context) {
        super(context);
    }

    public TimHeader(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TimHeader(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN && TimUtils.eventInView(mImage, event)) {
            mImage.callOnClick();
        }
        return true;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mText = findViewById(R.id.header_text);
        mImage = findViewById(R.id.header_img);
    }

    public void setText(CharSequence text) {
        mText.setText(text);
    }

    public void setText(int resId) {
        mText.setText(resId);
    }

    public void setImage(int resId) {
        mImage.setBackgroundResource(resId);
    }

    public void setImageClickListener(View.OnClickListener listener) {
        mImage.setOnClickListener(listener);
    }

}
